package com.gym.myshop.services.impl;

import com.gym.myshop.dao.AddressDao;
import com.gym.myshop.dao.impl.AddressDaoImpl;
import com.gym.myshop.entity.Address;
import com.gym.myshop.services.AddressSerives;
import com.gym.myshop.util.DBUtil;

import java.util.List;

/**
 * @author ming
 * 2021/11/7
 * description:
 */
public class AddressSerivesImpl implements AddressSerives {

    private AddressDao ad = new AddressDaoImpl();


    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:地址显示，降序排序 默认地址为排在第一
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public List<Address> getUserAddreeByUid(int uid) {
        try {
           DBUtil.begin();
            List<Address> al = ad.getUserAddressByUid(uid);
            DBUtil.commit();
            return al;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:删除地址 aid
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public boolean delAddress(Integer aid) {
        try {
            DBUtil.begin();
            int i = 0;
            if (aid > 0) {
                i = ad.delAddress(aid);
            }
            DBUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:添加地址
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public boolean insertAddress(Address a) {
        try {
            DBUtil.begin();
            int i = ad.insertAddress(a);
            DBUtil.commit();
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:修改地址
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public boolean updateAddress(Address a) {
        try {
            DBUtil.begin();
            int i = ad.updateAddress(a);
            DBUtil.commit();
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:设置默认地址为1，非默认astate = 0
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public boolean setDeafult(Integer aid, Integer uid) {
        try {
            DBUtil.begin();
            //1 把aid以外的所有用户地址设置默认为0
            int i = ad.setAllAddress(aid,uid);
            //2 把aid的地址设置为默认1
            int i2 = ad.setAddressDefault(aid);
            DBUtil.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }
}
